package com.ebelter.seneo.ui.activitys;

import android.content.Context;
import android.content.Intent;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.ebelter.btcomlib.models.https.NetUtils;
import com.ebelter.btcomlib.models.https.interfaces.HttpResponse;
import com.ebelter.btcomlib.models.https.responses.BaseResponse;
import com.ebelter.btcomlib.utils.MD5Utils;
import com.ebelter.btcomlib.utils.ToastUtil;
import com.ebelter.btcomlib.utils.UserSpUtil;
import com.ebelter.btcomlib.utils.VerifyUtils;
import com.ebelter.btcomlib.utils.ViewUtils;
import com.ebelter.seneo.R;
import com.ebelter.seneo.contants.IUserContant;
import com.ebelter.seneo.models.SeneoNet;
import com.ebelter.seneo.ui.fragments.finpsw.Fill_Psw_Fragment;
import com.ebelter.seneo.ui.fragments.finpsw.GetEmailCode_Fragment;

public class SeneoModifyPswActivity extends SeneoBaseActivity implements View.OnClickListener {


    public static final String EMAIL = "1";
    public static final String NAME = "2";


    private String email;
    private String nickName;

    public static void openSeneoModifyPswActivity(Context context,String email,String name){
        Intent intent=new Intent(context,SeneoModifyPswActivity.class);
        intent.putExtra(EMAIL,email);
        intent.putExtra(NAME,name);
        context.startActivity(intent);
    }


    ImageView top_iv;
    TextView top_tv1;


    private static final int F_GET_EMAIL_CODE=0;
    private static final int F_FILL_PSW=1;

    private int currentDisplayFragment;

    FrameLayout modifypsw_container;
    TextView modify_psw_next_bt;

    @Override
    protected int loadLayoutById() {
        return R.layout.activity_common1_ly;
    }

    @Override
    protected void initView() {
       // StatusBarUtil.setColor(this, Color.parseColor("#ff0000"));
        top_iv=findViewById(R.id.top_iv);
        top_tv1=findViewById(R.id.top_tv1);
        ViewUtils.setTextViewStr(top_tv1,"Password");
        modifypsw_container=findViewById(R.id.modifypsw_container);
        modify_psw_next_bt=findViewById(R.id.modify_psw_next_bt);

        modify_psw_next_bt.setEnabled(false);//默认为不可点击 等发送了邮箱验证码以后才能使用

        ViewUtils.displayView(top_iv);
        ViewUtils.displayView(top_tv1);

        top_iv.setOnClickListener(this);
        modify_psw_next_bt.setOnClickListener(this);

       setNextBtEnabled(true);
        //setNextBtEnabled(false);
    }


    @Override
    protected void initData() {

        email=getIntent().getStringExtra(EMAIL);
        nickName=getIntent().getStringExtra(NAME);

        displayFragment(F_GET_EMAIL_CODE);
    }

    public String getEmail() {
        return email;
    }

    private GetEmailCode_Fragment mGetEmailCode_Fragment;
    private Fill_Psw_Fragment mFill_Psw_Fragment;

    private void displayFragment(int whichFragment) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideAllFragment(transaction, mGetEmailCode_Fragment, mFill_Psw_Fragment);//隐藏全部的fragment

        int contain = R.id.modifypsw_container;//容器
        if (whichFragment==F_GET_EMAIL_CODE){

            if (mGetEmailCode_Fragment == null) {
                mGetEmailCode_Fragment = new GetEmailCode_Fragment();
                transaction.add(contain, mGetEmailCode_Fragment);
            } else {
                transaction.show(mGetEmailCode_Fragment);
            }
            ViewUtils.setTextViewStr(modify_psw_next_bt,"Next");
        }else if (whichFragment==F_FILL_PSW){
            if (mFill_Psw_Fragment == null) {
                mFill_Psw_Fragment = new Fill_Psw_Fragment();
                transaction.add(contain, mFill_Psw_Fragment);
            } else {
                transaction.show(mFill_Psw_Fragment);
            }
            ViewUtils.setTextViewStr(modify_psw_next_bt,"Done");
        }
        currentDisplayFragment=whichFragment;
        transaction.commit();


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.top_iv:
                back();
                break;
            case R.id.modify_psw_next_bt:
                next();
                break;
        }
    }

    private void next() {

        if (currentDisplayFragment==F_GET_EMAIL_CODE){

            String email = mGetEmailCode_Fragment.getEmail();
            String emailCode = mGetEmailCode_Fragment.getEmailCode();

            if (TextUtils.isEmpty(email)){
                ToastUtil.show(R.string.Email_must_not_be_empty);
                return;
            }
            if (!VerifyUtils.emailFormat(email)){
                ToastUtil.show(R.string.Incorrect_email_format);
                return;
            }

            if (TextUtils.isEmpty(emailCode)){
                ToastUtil.show(R.string.Mail_verification_code_cannot_be_empty);
                return;
            }



            displayFragment(F_FILL_PSW);
        }else {
            done();
        }

    }

    //完成按钮
    private void done() {




        String email = mGetEmailCode_Fragment.getEmail();
        String emailCode = mGetEmailCode_Fragment.getEmailCode();





        if (TextUtils.isEmpty(email)){
            ToastUtil.show(R.string.Email_must_not_be_empty);
            return;
        }
        if (!VerifyUtils.emailFormat(email)){
            ToastUtil.show(R.string.Incorrect_email_format);
            return;
        }

        if (TextUtils.isEmpty(emailCode)){
            ToastUtil.show(R.string.Mail_verification_code_cannot_be_empty);
            return;
        }



        final String psw1=mFill_Psw_Fragment.getPsw1();
        String psw2=mFill_Psw_Fragment.getPsw2();

        if (TextUtils.isEmpty(psw1)){
            ToastUtil.show(R.string.The_password_cannot_be_empty);
            return;
        }
        if (psw1.length()<6){
            ToastUtil.show("Password contains at least 6 characters");
            return;
        }
        if (!TextUtils.equals(psw1,psw2)){
            ToastUtil.show(R.string.The_two_passwords_are_inconsistent);
            return;
        }

        if (!NetUtils.available()){
            ToastUtil.show(R.string.Network_error);
            return;
        }

        final String md5psw=MD5Utils.getMd5(psw1);

        String srcPsw = UserSpUtil.readString(IUserContant.IUser.psw);
        if (TextUtils.equals(md5psw,srcPsw)){
            ToastUtil.show("The new password cannot be the same as the old password. Please reset your new password.");
            return;
        }




        SeneoNet.getIntance().findPassWord(this, email, md5psw, emailCode, new HttpResponse<BaseResponse>() {
            @Override
            public void result(boolean isRequestSuccess, String requestDes, BaseResponse baseResponse,String s) {
                if (isRequestSuccess){
                    if (baseResponse.resultCode==0){
                        ToastUtil.show("Password reset successfully");

                        if (!TextUtils.isEmpty(nickName)){
                            UserSpUtil.writeString(IUserContant.IUser.loginName,nickName);
                            UserSpUtil.writeString(IUserContant.IUser.psw,md5psw);
                            UserSpUtil.writeString(IUserContant.IUser.psw2,psw1);
                        }else {
                            UserSpUtil.writeString(IUserContant.IUser.psw,null);
                            UserSpUtil.writeString(IUserContant.IUser.psw2,null);
                        }

                        finish();
                    }else if (baseResponse.resultCode==117){
                        ToastUtil.show(R.string.Email_is_not_registered);
                    }else if (baseResponse.resultCode==106){
                        ToastUtil.show(R.string.Email_verification_code_has_expired);

                    }else if (baseResponse.resultCode==107){
                        ToastUtil.show(R.string.Wrong_email_verification_code);
                    }else if(baseResponse.resultCode==110){
                        ToastUtil.show(R.string.An_exception_occurred_on_the_server);
                    }else {
                        ToastUtil.show("Password reset failed");
                    }
                }else {
                    ToastUtil.show(R.string.Network_connection_error);
                }
            }
        });





    }

    public void setNextBtEnabled(boolean enabled){
        if (modify_psw_next_bt!=null){
            modify_psw_next_bt.setEnabled(enabled);
        }
    }


    //点击了回退键
    private void back() {
        if (currentDisplayFragment==F_GET_EMAIL_CODE){
            finish();
        }else if (currentDisplayFragment==F_FILL_PSW){

            clearPsw();
            displayFragment(F_GET_EMAIL_CODE);
        }
    }

    private void clearPsw() {
        if (mFill_Psw_Fragment!=null){
            mFill_Psw_Fragment.clearPsw();
        }
    }


    @Override
    public void onBackPressed() {
        back();
    }
}
